package com.chance.cc.crawler.development.bootstrap.kaoyan;

import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2020/12/9 16:04
 * @Description
 *      考研论坛
 **/
public class Kaoyan {
    public static void main(String[] args) {
        String domain = "kaoyan";
        String articleEntranceUrl = "http://bbs.kaoyan.com/forum.php?mod=forumdisplay&fid=67&filter=author&orderby=dateline";
        String interactionOrCommentEntranceUrl = "http://bbs.kaoyan.com/forum.php?mod=forumdisplay&fid=67&filter=lastpost&orderby=lastpost";

        DevCrawlerController.builder()
                .triggerInfo(domain,domain,System.currentTimeMillis(),domain)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domain))
                .consoleResultPipeline()//控制台输出
//                .fileResultPipeline("D:\\chance\\log\\tets.log",true) //文件输出
//                .requestRecord(createCrawlerRequestArticleRecord(domain,articleEntranceUrl))
                .requestRecord(createCrawlerRequestInteractionCommentRecord(domain,interactionOrCommentEntranceUrl))
                .build()
                .start();
    }

    /**
     * 采集文章record
     * @param domain
     * @param url
     * @return
     */
    public static CrawlerRequestRecord createCrawlerRequestArticleRecord(String domain,String url){
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .httpUrl(url)
                .recordKey(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null))
                .releaseTime(System.currentTimeMillis())
//                .proxy(proxy)
                .build();


        crawlerRequestRecord.tagsCreator().bizTags().addSite("article");
        //控制下载文章的时候是否下载评论和互动量
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("interaction","interaction");
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment","comment");

        return crawlerRequestRecord;
    }

    /**
     * 单独采集互动量或者回复record
     * @param domain
     * @param url
     * @return
     */
    public static CrawlerRequestRecord createCrawlerRequestInteractionCommentRecord(String domain,String url){
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .httpUrl(url)
                .recordKey(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
//                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null))
                .releaseTime(System.currentTimeMillis())
//                .proxy(proxy)
                .build();

        crawlerRequestRecord.tagsCreator().bizTags().addSite("interactionOrComment");
        //控制下载文章的时候是否下载评论和互动量
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("interaction","interaction");
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment","comment");

        return crawlerRequestRecord;
    }
}
